<?php

namespace app\common\model;

use app\common\enum\order\PayStatus as PayStatusEnum;
use app\common\enum\order\Status as OrderStatusEnum;
use app\common\enum\order\PayType as OrderPayType;
use think\Db;

/**
 * 财务管理模型
 * Class Order
 * @package app\common\model
 */
class Finance extends BaseModel
{


    /**
     * 总流水
     * Class Order
     * @package app\common\model
     */
    public function getTotalPay($startDate = null, $endDate = null){

        //订单表
        $orderPayPay = $this->getOrderPay($startDate, $endDate);
        //爆品礼订单
        $giftsPay = $this->getGiftsMoney($startDate, $endDate);

        return bcadd($orderPayPay, $giftsPay, 2);

    }


    /**
     * 订单支付
     * Class Order
     * @package app\common\model
     */
    public function getOrderPay($startDate = null, $endDate = null){

        $filter = [
            'pay_status' => PayStatusEnum::SUCCESS,
            'wxapp_id' => self::$wxapp_id,
        ];

        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }

        $money = Db::name('order')->where($filter)->sum('pay_price');

        return bcadd($money, 0, 2);

    }


    /**
     * 微信支付
     * Class Order
     * @package app\common\model
     */
    public function getWechatPay($startDate = null, $endDate = null){

        $filter = [
            'pay_status' => PayStatusEnum::SUCCESS,
            'pay_type' => OrderPayType::WECHAT,
            'wxapp_id' => self::$wxapp_id,
        ];

        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }

        $wechatmoney = Db::name('order')->where($filter)->sum('pay_price');
        $gifts_order_money = Db::name('gifts_order')->where($filter)->sum('pay_price');

        return bcadd($wechatmoney, $gifts_order_money, 2);

    }



    /**
     * 余额支付
     * Class Order
     * @package app\common\model
     */
    public function getMoneyPay($startDate = null, $endDate = null){

        $filter = [
            'pay_status' => PayStatusEnum::SUCCESS,
            'pay_type' => OrderPayType::BALANCE,
            'wxapp_id' => self::$wxapp_id,
        ];

        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }

        $wechatmoney = Db::name('order')->where($filter)->sum('pay_price');
        $gifts_order_money = Db::name('gifts_order')->where($filter)->sum('pay_price');
        return bcadd($wechatmoney, $gifts_order_money, 2);
    }



    /**
     * 实收金额(微信实收+余额实收)
     * Class Order
     * @package app\common\model
     */
    public function getActualPay($startDate = null, $endDate = null){

        //微信实收
        $wechat_money = $this->getWechatActualPay($startDate,$endDate);
        //余额实收
        $money = $this->getMoneyActualPay($startDate,$endDate);

        return bcadd($wechat_money, $money, 2);

    }



    /**
     * 微信实收(微信总支付-分红-分销-微信支付退货完成的)
     * Class Order
     * @package app\common\model
     */
    public function getWechatActualPay($startDate = null, $endDate = null){

        //微信总支付
        $wechat_money = $this->getWechatPay($startDate,$endDate);
        //分红金额
        $bonus_money = $this->getBonusMoney($startDate,$endDate);
        //分销金额
        $dealcer_money = $this->getDealcerMoney($startDate,$endDate);
        //微信支付退货完成的
        $wechat_back_money = $this->getWeChatBackMoney($startDate,$endDate);

        $out_money =  bcadd($bonus_money, 0, 2);
        $out_all_money =  bcadd($out_money, $wechat_back_money, 2);

        return bcsub($wechat_money, $out_all_money, 2);

    }



    /**
     * 余额实收(余额支付总和-余额支付退货完成的)
     * Class Order
     * @package app\common\model
     */
    public function getMoneyActualPay($startDate = null, $endDate = null){

        $filter = [
            'wxapp_id' => self::$wxapp_id,
            'pay_type' => OrderPayType::BALANCE,
            'pay_status' => PayStatusEnum::SUCCESS,
        ];
        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }


        $money = Db::name('order')->where($filter)->sum('pay_price');

        $gifts_order_money = Db::name('gifts_order')->where($filter)->sum('pay_price');
        //余额支付总和
        $all_money = bcadd($money, $gifts_order_money, 2);
        //微信支付退货完成的
        $money_back_money = $this->getMoneyBackMoney($startDate,$endDate);

        return bcsub($all_money, $money_back_money, 2);

    }



    /**
     * 微信分红金额
     * Class Order
     * @package app\common\model
     */
    public function getBonusMoney($startDate = null, $endDate = null){

        $filter = [
            'wxapp_id' => self::$wxapp_id,
        ];
        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['create_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }
        $money = Db::name('divide_receives')->where($filter)->sum('refund_money');

        return bcadd($money, 0, 2);

    }

    /**
     * 分红余额
     * Class Order
     * @package app\common\model
     */
    public function bonusOutMoney($startDate = null, $endDate = null){

        $filter = [
            'wxapp_id' => self::$wxapp_id,
        ];
        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['create_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }
        $money = Db::name('divide_receives')->where($filter)->sum('reward_money');

        return bcadd($money, 0, 2);

    }


    /**
     * 分红未出局
     * Class Order
     * @package app\common\model
     */
    public function getbonusMoneyNo($startDate = null, $endDate = null){

        $filter = [
            'wxapp_id' => self::$wxapp_id,
            'type'=>0
        ];
        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['create_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }
        $money = Db::name('divide_receives')->where($filter)->sum('refund_money');

        return bcadd($money, 0, 2);

    }


    /**
     * 分红已出局
     * Class Order
     * @package app\common\model
     */
    public function getbonusMoneyOut($startDate = null, $endDate = null){

        $filter = [
            'wxapp_id' => self::$wxapp_id,
            'type'=>1
        ];
        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['create_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }
        $money = Db::name('divide_receives')->where($filter)->sum('refund_money');

        return bcadd($money, 0, 2);

    }



    /**
     * 分销金额
     * Class Order
     * @package app\common\model
     */
    public function getDealcerMoney($startDate = null, $endDate = null){

        $filter = [
            'wxapp_id' => self::$wxapp_id,
            'is_settled'=>1
        ];
        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['settle_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }
        $money = Db::name('dealer_order')->where($filter)->sum('first_money');

        return bcadd($money, 0, 2);

    }



    /**
     * 商品支付金额
     * Class Order
     * @package app\common\model
     */
    public function getGoodsMoney($startDate = null, $endDate = null){

        $filter = [
            'pay_status' => PayStatusEnum::SUCCESS,
            'order_source' => 10,
            'wxapp_id' => self::$wxapp_id,
        ];
        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }
        $money = Db::name('order')->where($filter)->sum('pay_price');

        return bcadd($money, 0, 2);

    }



    /**
     * 砍价支付金额
     * Class Order
     * @package app\common\model
     */
    public function getBargainingMoney($startDate = null, $endDate = null){

        $filter = [
            'pay_status' => PayStatusEnum::SUCCESS,
            'order_source' => 20,
            'wxapp_id' => self::$wxapp_id,
        ];
        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }
        $money = Db::name('order')->where($filter)->sum('pay_price');

        return bcadd($money, 0, 2);

    }


    /**
     * 秒杀支付金额
     * Class Order
     * @package app\common\model
     */
    public function getSeckillMoney($startDate = null, $endDate = null){

        $filter = [
            'pay_status' => PayStatusEnum::SUCCESS,
            'order_source' => 30,
            'wxapp_id' => self::$wxapp_id,
        ];
        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }
        $money = Db::name('order')->where($filter)->sum('pay_price');

        return bcadd($money, 0, 2);

    }



    /**
     * 拼团支付金额
     * Class Order
     * @package app\common\model
     */
    public function getTeamMoney($startDate = null, $endDate = null){

        $filter = [
            'pay_status' => PayStatusEnum::SUCCESS,
            'order_source' => 40,
            'wxapp_id' => self::$wxapp_id,
        ];
        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }
        $money = Db::name('order')->where($filter)->sum('pay_price');

        return bcadd($money, 0, 2);

    }


    /**
     * 储值支付金额
     * Class Order
     * @package app\common\model
     */
    public function getVipMoney($startDate = null, $endDate = null){

        $filter = [
            'pay_status' => PayStatusEnum::SUCCESS,
            'order_source' => 40,
            'wxapp_id' => self::$wxapp_id,
        ];
        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }
        $money = Db::name('order')->where($filter)->sum('pay_price');

        return bcadd($money, 0, 2);

    }


    /**
     * 当面付支付金额
     * Class Order
     * @package app\common\model
     */
    public function getFaceMoney($startDate = null, $endDate = null){

        $filter = [
            'pay_status' => PayStatusEnum::SUCCESS,
            'order_source' => 50,
            'wxapp_id' => self::$wxapp_id,
        ];
        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }
        $money = Db::name('order')->where($filter)->sum('pay_price');

        return bcadd($money, 0, 2);

    }




    /**
     * 爆品礼支付金额
     * Class Order
     * @package app\common\model
     */
    public function getGiftsMoney($startDate = null, $endDate = null){

        $filter = [
            'pay_status' => PayStatusEnum::SUCCESS,
            'wxapp_id' => self::$wxapp_id,
        ];

        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }

        $money = Db::name('gifts_order')->where($filter)->sum('pay_price');

        return bcadd($money, 0, 2);

    }


    /**
     * 实物商品支付
     * Class Order
     * @package app\common\model
     */
    public function getTrueGoodsMoney($startDate = null, $endDate = null){

        $filter = [
            'pay_status' => PayStatusEnum::SUCCESS,
            'wxapp_id' => self::$wxapp_id,
            'is_fictitious' => 2,
            'pay_type' => 20,
        ];

        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }

        $money = Db::name('order')->where($filter)->sum('pay_price');

        return bcadd($money, 0, 2);

    }


    /**
     * 虚拟商品支付
     * Class Order
     * @package app\common\model
     */
    public function getFalseGoodsMoney($startDate = null, $endDate = null){

        $filter = [
            'pay_status' => PayStatusEnum::SUCCESS,
            'wxapp_id' => self::$wxapp_id,
            'is_fictitious' => 1,
            'pay_type' => 20,
        ];

        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }

        $money = Db::name('order')->where($filter)->sum('pay_price');

        return bcadd($money, 0, 2);

    }



    /**
     * 收益支付
     * Class Order
     * @package app\common\model
     */
    public function getIncomeMoney($startDate = null, $endDate = null){

        $filter = [
            'pay_status' => PayStatusEnum::SUCCESS,
            'wxapp_id' => self::$wxapp_id,
            'pay_way'=>20
        ];

        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }

        $money = Db::name('order')->where($filter)->sum('pay_price');

        return bcadd($money, 0, 2);

    }


    /**
     * 已打款金额
     * Class Order
     * @package app\common\model
     */
    public function getWithdrawMoney($startDate = null, $endDate = null){

        $filter = [
            'apply_status' => 40,
            'wxapp_id' => self::$wxapp_id,
        ];

        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['audit_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }

        $money = Db::name('dealer_withdraw')->where($filter)->sum('amount');

        return bcadd($money, 0, 2);

    }




    /**
     * 退款金额
     * Class Order
     * @package app\common\model
     */
    public function getBackMoney($startDate = null, $endDate = null){

        $filter = [
            'pay_status' => PayStatusEnum::SUCCESS,
            'order_status' => OrderStatusEnum::CANCELLED,
            'wxapp_id' => self::$wxapp_id,
        ];

        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }

        $money = Db::name('order')->where($filter)->sum('pay_price');

        return bcadd($money, 0, 2);

    }



    /**
     * 微信支付退款金额
     * Class Order
     * @package app\common\model
     */
    public function getWeChatBackMoney($startDate = null, $endDate = null){

        $filter = [
            'pay_status' => PayStatusEnum::SUCCESS,
            'order_status' => OrderStatusEnum::CANCELLED,
            'wxapp_id' => self::$wxapp_id,
            'pay_type' => OrderPayType::BALANCE,
        ];

        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }

        $money = Db::name('order')->where($filter)->sum('pay_price');

        return bcadd($money, 0, 2);

    }



    /**
     * 余额支付退款金额
     * Class Order
     * @package app\common\model
     */
    public function getMoneyBackMoney($startDate = null, $endDate = null){

        $filter = [
            'pay_status' => PayStatusEnum::SUCCESS,
            'order_status' => OrderStatusEnum::CANCELLED,
            'wxapp_id' => self::$wxapp_id,
            'pay_type' => OrderPayType::WECHAT,
        ];

        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['pay_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }

        $money = Db::name('order')->where($filter)->sum('pay_price');

        return bcadd($money, 0, 2);

    }



    /**
     * 零钱总金额
     * Class Order
     * @package app\common\model
     */
    public function getSmallChange($startDate = null, $endDate = null){

        $filter = [
            'wxapp_id' => self::$wxapp_id,
        ];

        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['update_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }

        $money = Db::name('divide_receives')->where($filter)->sum('small_change');

        return bcadd($money, 0, 2);

    }



    /**
     * 零钱抵扣金额
     * Class Order
     * @package app\common\model
     */
    public function getBalanceMoney($startDate = null, $endDate = null){

        $filter = [
            'pay_status' => PayStatusEnum::SUCCESS,
            'wxapp_id' => self::$wxapp_id,
            'pay_type' => OrderPayType::WECHAT,
        ];

        if (!is_null($startDate) && !is_null($endDate)) {
            $filter['update_time'] = [
                ['>=', strtotime($startDate)],
                ['<', strtotime($endDate)],
            ];
        }

        $money = Db::name('order')->where($filter)->sum('balance_money');

        return bcadd($money, 0, 2);

    }



}
